package com.chif.mapreduce.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class WordCountReducer extends Reducer<Text, IntWritable,Text,IntWritable> {

    //懒汉式
    int sum;
    IntWritable v=new IntWritable();

    /**
     * ReduceTask进程对每一组相同k的<k,v>调用一次reduce()方法
     * @param key
     * @param values
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

        //1 累加求和
        sum=0;
        for (IntWritable count : values) {
            sum+=count.get();
        }

        //2 输出
        v.set(sum);
        context.write(key,v);
    }
}
